import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
const {
    globalData: {
        Api: {
            cart: {
                list_ids
            },
            address: {
                getList,
                get_default
            },
            order: {
                addOrderList
            }
        }
    }
} = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        ids: [],
        productList: [],
        addressData: null
    },
    selectAddres() {
        wx.navigateTo({
            url: '/pages/address/address',
            events: {
                fromOrderConfirm: data => {
                    this.setData({
                        addressData: data
                    });
                }
            }
        });
    },
    async getAdress() {
        // 首先获取默认地址
        let addressData = await get_default();
        if (addressData) {
            this.setData({
                addressData
            });
            return;
        }
        // 获取全部地址
        let addressList = await getList();
        if (addressList.length > 0) {
            this.setData({
                addressData: addressList[0]
            });
        }

    },
    /**
     * 
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        const eventChannel = this.getOpenerEventChannel();
        eventChannel.on('settleIds', async (data) => {
            let {
                ids
            } = data;
            let list = await list_ids({
                ids
            });
            this.setData({
                ids,
                productList: list
            });
        });
        this.getAdress();
    },
    submitOrder(event) {
        Dialog.confirm({
                title: '订单提交提醒',
                message: '确认要提交订单吗',
            })
            .then(async () => {
                // on confirm
                if (!this.data.addressData) {
                    wx.showToast({
                        title: '请先添加地址',
                        icon: 'none',
                    });
                    return;

                }
                let data = {
                    ids: this.data.ids,
                    account: event.target.dataset.account,
                    address_id: this.data.addressData.id
                }
                let orderId = await addOrderList(data);
                wx.showToast({
                    title: '提交成功',
                    icon: 'success',
                    duration: 1500,
                    mask: false,
                    success: async (result) => {
                        await new Promise(resolve => {
                            setTimeout(() => {
                                resolve();
                            }, 1500);
                        });
                        wx.redirectTo({
                            url: `/pages/orderPay/orderPay?orderId=${orderId}`,
                            success: (result) => {

                            },
                            fail: () => {},
                            complete: () => {}
                        });
                    },
                    fail: () => {},
                    complete: () => {}
                });

            })
            .catch(() => {
                // on cancel
            });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})